package com.loe.test

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import cn.jzvd.LoeVideoPlayer
import com.loe.camera.*
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


//        fun cameraSizeToSting(sizes: List<Camera.Size>): String
//        {
//            val s = StringBuilder()
//            for (size in sizes)
//            {
//                if (s.length != 0) s.append(",\n")
//                s.append(size.width).append('x').append(size.height)
//            }
//            return s.toString()
//        }
//
//        val cameraInfo: Camera.CameraInfo = Camera.CameraInfo()
//        for (cameraId in 0 until Camera.getNumberOfCameras())
//        {
//            Camera.getCameraInfo(cameraId, cameraInfo)
//            val camera: Camera = Camera.open(cameraId)
//            val params: Camera.Parameters = camera.getParameters()
//            val previewSizes: List<Camera.Size> = params.getSupportedVideoSizes()
//            textView.text = cameraSizeToSting(previewSizes)
//        }

//        PermissionUtil.request(this, PermissionUtil.STORAGE)

        buttonVideo.setOnClickListener()
        {
//            LoeCamera.takeVideo(
//                this, VideoConfig()
//                    .setBitRate(6000)
//                    .setFront(true)
//                    .setMaxSeconds(10)
//                    .setShowAlbum(true)
//            )
//            {
//                textView.text = it
//
////                jzVideo.setUp(it, name)
////                jzVideo.startVideo()
//
//                val name = it.split("/").last().split(".")[0]
//                LoeVideoPlayer.play(this, it, name, false)
//            }

            TakeVideoDialog(this)
                .config(VideoConfig().setMaxSeconds(30 * 60))
                .setOnPathCallback()
                {
                    val name = it.split("/").last().split(".")[0]
                    LoeVideoPlayer.play(this, it, name, false)
                }
                .show()
        }

        var url = "http://192.168.1.106/test/%E7%AB%A0%E8%B7%AF%E9%A1%BA-%E8%BD%AC%E6%AD%A3%E7%94%B3%E8%AF%B7%E8%A1%A8.docx";

        buttonPhoto.setOnClickListener()
        {
//            LoeCamera.takePhoto(this, PhotoConfig().setFront(true))
//            {
//                LoeCamera.cropImage(this, it, true)
//                {
//                    textView.text = "" + it + File(it).exists()
//                }
//            }

//            TakePhotoDialog(this, PhotoConfig().setErrorCallback { Toast.makeText(this, "错误", Toast.LENGTH_SHORT).show() })
//            {
//                textView.text = it
//            }.show()

            TakePhotoDialog(this).setOnPathCallback()
            {
                LoeCamera.cropImage(this, it, true)
                {
                    textView.text = "" + it + File(it).exists()
                }
            }.show()

//            LoeCamera.takePhotoSysCamera(this, PhotoConfig())
//            {
//                LoeCamera.cropImage(this, it)
//                {
//                    textView.text = it
//                }
//            }


//            LoeCamera.takePhoto(this, PhotoConfig().setSavePath(CameraImgUtil.getPhotoTemp()))
//            {
//                LoeCamera.cropImage(this, it)
//                {
//                    textView.text = it
//                }
//            }

//            LoeCamera.takeFile(this)
//            {
//                textView.text = it
//                url = it
//            }
        }
//        buttonPhoto.performClick()


        val apk = Environment.getExternalStorageDirectory()!!.absolutePath+"/DCIM/test.docx"

        buttonOpen.setOnClickListener()
        {
            LoeCamera.open(this, "http://192.168.137.1:8080/test/test.docx")

//            try
//            {
//                val intent = Intent(Intent.ACTION_VIEW)
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                val file = File(apk)
//                // 7.0+ 以上版本
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
//                {
//                    val apkUri = FileProvider.getUriForFile(this, "$packageName.camerafileprovider", file)
//                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
//                    intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
//                } else
//                {
//                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
//                }
//                startActivity(intent)
//            } catch (e: Exception)
//            {
//                Log.e("runtime", e.toString())
//            }
        }


        val m3u8Url = "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"

        val name = m3u8Url.split("/").last().split(".")[0]
        jzVideo.setUp(m3u8Url, name)
//        jzVideo.posterImageView.setImageURI(
//            Uri.parse("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2330582835,4253854595&fm=26&gp=0.jpg"))

        textView.text = Build.BRAND + " - " + Build.MANUFACTURER
    }
}